package us.nchu.tomtiger.service;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Service;
import us.nchu.tomtiger.dao.CollectionDao;
import us.nchu.tomtiger.dao.ProjectDao;
import us.nchu.tomtiger.model.Collection;
import us.nchu.tomtiger.model.Project;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by XiongXuan on 2018/1/8.
 */
@Service
public class CollectionService {
    @Resource
    private ProjectDao projectDao;

    @Resource
    private CollectionDao collectionDao;

    public void createCollection(Collection collection) {
        collectionDao.insert(collection);
    }

    public void deleteCollection(String phoneNumber, int projectId) {
        collectionDao.delete(phoneNumber, projectId);
    }

    public void deleteCollection(String phoneNumber, String projectId) {
        collectionDao.delete(phoneNumber, Integer.parseInt(projectId));
    }

    public List<Collection> getListByPhoneNumber(String phoneNumber) {

//        System.out.println("=1=================");
//        List<Collection> collections = collectionDao.queryByUserForList(phoneNumber);
//        for (Collection collection : collections) System.out.println(collection);
//        System.out.println("=2=================");

        return collectionDao.queryByUserForList(phoneNumber);
    }

    public List getListByProjectId(String projectId) {
        return collectionDao.queryByProjectForList(projectId);
    }

    public List getListByProjectId(int projectId) {
        return collectionDao.queryByProjectForList(projectId);
    }

    public boolean isCollected(String phoneNumber, int projectId) {
        try {
            collectionDao.query(phoneNumber, projectId);
        }catch (EmptyResultDataAccessException e) {
            return false;
        }
        return true;
    }

}
